package com.shujia.day19;

import java.util.Timer;
import java.util.TimerTask;

/*
    定时器：Timer
    定时任务：TimerTask
 */
public class TimerDemo1 {
    public static void main(String[] args) {
        //创建一个定时器
        Timer timer = new Timer();

        //public void schedule(TimerTask task, long delay) 延迟多少毫秒后执行定义任务
        timer.schedule(new MyTask(timer), 5000);

        //public void schedule(TimerTask task,long delay,long period) 延迟delay毫秒后执行定义任务，后续每间隔period毫米执行一次
        timer.schedule(new MyTask(timer), 5000,2000);
    }
}

class MyTask extends TimerTask{
    Timer timer;

    public MyTask(Timer timer) {
        this.timer = timer;
    }

    @Override
    public void run() {
        System.out.println("砰！爆炸了.....");
//        timer.cancel();
    }
}
